//
//  AnchorVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "AnchorVC.h"
#import "RecommendModel.h"
#import "ListModel.h"
#import "ExampleModel.h"
#import "AnchorCollectionViewCell.h"
#import "RecommendCollectionReusableView.h"
#import "JGAnchorDigestVC.h"
#import "MoreAnchorVC.h"

@interface AnchorVC ()<UICollectionViewDataSource, UICollectionViewDelegate, MoreAnchorTPDelegat>

@property (nonatomic, strong) UICollectionView *backCollectionV;/**< 底层collectionView */
@property (nonatomic, strong) NSMutableArray *modelArr;/**< model数组 */
@property (nonatomic, assign) BOOL isUpState;/**< 标记上拉刷新 */

@end

@implementation AnchorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.modelArr = [NSMutableArray array];
    
//    [self dataHander];
    self.isUpState = NO;
    [self createView];
    [self addHeader];
    [self addFooter];

    
    
}
- (void)createView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 41) / 3, (SCREEN_WIDTH - 40) / 3 + 30);
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    
    self.backCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 153) collectionViewLayout:layout];
    [self.view addSubview:_backCollectionV];
    self.backCollectionV.delegate = self;
    self.backCollectionV.dataSource = self;
    self.backCollectionV.backgroundColor = [UIColor whiteColor];
    
    // 注册cell和header
    [self.backCollectionV registerClass:[AnchorCollectionViewCell class] forCellWithReuseIdentifier:@"AnchorCollectionViewCell"];
    [self.backCollectionV registerClass:[RecommendCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RecommendCollectionReusableView"];
    
}
#pragma mark - collectionview的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.modelArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AnchorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnchorCollectionViewCell" forIndexPath:indexPath];
    ListModel *model = [[ListModel alloc] init];
    model = self.modelArr[indexPath.section];
    ExampleModel *model2 = [[ExampleModel alloc] init];
    model2 = model.list[indexPath.row];
    
    cell.model = model2;
    
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        RecommendCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RecommendCollectionReusableView" forIndexPath:indexPath];

        ListModel *model = [[ListModel alloc] init];
        model = self.modelArr[indexPath.section];
        
        header.AnchorDelegat = self;
        
            [header setTitle:model.title];
        
        return header;
    }
    
    return nil;

}
// collectionViewcell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JGAnchorDigestVC *anchorDigestVC = [[JGAnchorDigestVC alloc] init];
    ListModel *model = [[ListModel alloc] init];
    model = self.modelArr[indexPath.section];
    ExampleModel *model2 = [[ExampleModel alloc] init];
    model2 = model.list[indexPath.row];
    
    anchorDigestVC.uid = model2.uid;
    anchorDigestVC.title = model2.title;
    anchorDigestVC.currentPage = model2.currentPage;
    
    [self.navigationController pushViewController:anchorDigestVC animated:YES];
    
}
// more 跳转的协议方法
- (void)clickToMore:(NSString *)clickTitle MoreTitle:(NSString *)title{
    MoreAnchorVC *moreAnchorVC = [[MoreAnchorVC alloc] init];
    
    moreAnchorVC.name = clickTitle;
    
    moreAnchorVC.myTitle = title;
    
    [self.navigationController pushViewController:moreAnchorVC animated:YES];
}
#pragma mark - 数据解析
- (void)dataHander{
    NSString *path = @"http://mobile.ximalaya.com/m/explore_user_index?device=iPhone&page=1";
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {

        NSArray *arr = [responseObject objectForKey:@"list"];
        
        if (_isUpState == NO) {
            
            self.modelArr = [ListModel modelHandleWithArray:arr];
        }else{
            for (NSDictionary *dic in arr) {
                ListModel *model = [[ListModel alloc] init];;
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArr addObject:model];
            }

        }
        
        [_backCollectionV reloadData];
        [_backCollectionV.mj_header endRefreshing];
        [_backCollectionV.mj_footer endRefreshing];
        
    } filed:^(NSError *error) {
        
        NSLog(@"解析失败");
        
    }];
}
/**< 下拉刷新 */
- (void)addHeader{

    self.backCollectionV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 标记为下拉
        self.isUpState = NO;

        
        [self dataHander];
        
    }];
    [self.backCollectionV.mj_header beginRefreshing];
    
}
/**< 上拉加载 */
- (void)addFooter{
    self.backCollectionV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 标记为上拉
        _isUpState = YES;
        
        [self dataHander];
        
    }];
    
    [self.backCollectionV.mj_footer beginRefreshing];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
